//
//  ViewController.m
//  test
//
//  Created by 高杰 on 2017/5/8.
//  Copyright © 2017年 高杰. All rights reserved.
//

#import "UM.h"
#import "UMMobClick/MobClick.h"
#import <UShareUI/UShareUI.h>
#import <UMSocialCore/UMSocialCore.h>

#define WEIXIN 1
#define QQ 2
#define WEIBO 3

#pragma mark - UM
@interface UM()

@end
@implementation UM

- (void)pluginInitialize{
    NSLog(@"UM->%@", @"init");

    UMConfigInstance.appKey = [self.commandDelegate.settings objectForKey:[@"UMENG_APPKEY" lowercaseString]];
    UMConfigInstance.ChannelId = [self.commandDelegate.settings objectForKey:[@"UMENG_CHANNEL" lowercaseString]];
    …  
    [MobClick startWithConfigure:UMConfigInstance];

    [[UMSocialManager defaultManager] setUmSocialAppkey:[self.commandDelegate.settings objectForKey:[@"UMENG_APPKEY" lowercaseString]]];

    [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;

    NSString *WEIXIN_KEY = [self.commandDelegate.settings objectForKey:[@"WEIXIN_KEY" lowercaseString]];
    NSString *WEIXIN_SECRET= [self.commandDelegate.settings objectForKey:[@"WEIXIN_SECRET" lowercaseString]];
    NSString *WEIXIN_REDIRCTURL = [self.commandDelegate.settings objectForKey:[@"WEIXIN_REDIRCTURL" lowercaseString]];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WEIXIN_ID appSecret:WEIXIN_SECRET redirectURL:WEIXIN_REDIRCTURL];

    NSString *QQ_ID = [self.commandDelegate.settings objectForKey:[@"QQ_ID" lowercaseString]];
    NSString *QQ_SECRET= [self.commandDelegate.settings objectForKey:[@"QQ_SECRET" lowercaseString]];
    NSString *QQ_REDIRECTURL = [self.commandDelegate.settings objectForKey:[@"qq_redirectURL" lowercaseString]];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQ_ID appSecret:QQ_SECRET redirectURL:QQ_REDIRECTURL];

    NSString *WEIBO_KEY = [self.commandDelegate.settings objectForKey:[@"WEIBO_KEY" lowercaseString]];
    NSString *WEIBO_SECRET= [self.commandDelegate.settings objectForKey:[@"WEIBO_SECRET" lowercaseString]];
    NSString *WEIBO_REDIRECTURL = [self.commandDelegate.settings objectForKey:[@"WEIBO_REDIRECTURL" lowercaseString]];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:WEIBO_KEY  appSecret:WEIBO_SECRET redirectURL:WEIBO_REDIRECTURL];
    
    // [[UMSocialManager defaultManager] openLog:YES];
}

-(void) openShare:(CDVInvokedUrlCommand*)command
{
    NSLog(@"UM->%@", @"openShare");
    NSDictionary* options;
    if (command.arguments.count == 0) {
        options = [NSDictionary dictionary];
    } else {
        options = command.arguments[0];
    }
    NSString *title = options[@"title"];
    NSString *imageUrl = options[@"imageUrl"];
    NSString *desc = options[@"desc"];
    NSString *link = options[@"link"];
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatSession)]];
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

        //创建网页内容对象
        NSString* thumbURL =  imageUrl;
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:desc thumImage:thumbURL];
        //设置网页地址
        shareObject.webpageUrl = link;

        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;

        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) {
            CDVPluginResult *pluginResult = nil;
            if (error) {
                NSLog(@"UM->%@:%@", @"openShare", @"error");
                pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error];
            }else{
                NSLog(@"UM->%@:%@", @"openShare", @"success");
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    UMSocialShareResponse *resp = data;
                    pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:resp.message];
                }else{
                    pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:data];
                }
            }
            [self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
        }];
    }];
}

-(void) auth:(CDVInvokedUrlCommand*)command
{
    NSLog(@"UM->%@", @"auth");
    NSDictionary* options;
    if (command.arguments.count == 0) {
        options = [NSDictionary dictionary];
    } else {
        options = command.arguments[0];
    }
    NSNumber *platform = options[@"platform"];
    UMSocialPlatformType platformType = [self getPlatform:platform];
    if (platformType == UMSocialPlatformType_UnKnown) return;
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self.viewController completion:^(id data, NSError *error) {
        CDVPluginResult *pluginResult = nil;
        if (error) {
            NSLog(@"UM->%@:%@", @"auth", @"error");
            pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:@"error"];
        }else{
            NSLog(@"UM->%@:%@", @"auth", @"success");
            if ([data isKindOfClass:[UMSocialUserInfoResponse class]]) {
                UMSocialUserInfoResponse *resp = data;
                NSDictionary *result = [NSDictionary alloc];
                [result setValue:resp.uid forKey:@"uid"];
                [result setValue:resp.openid forKey:@"openid"];
                [result setValue:resp.accessToken forKey:@"accessToken"];
                [result setValue:resp.refreshToken forKey:@"refreshToken"];
                [result setValue:resp.expiration forKey:@"expiration"];
                [result setValue:resp.name forKey:@"name"];
                [result setValue:resp.iconurl forKey:@"iconurl"];
                [result setValue:resp.unionGender forKey:@"unionGender"];
                pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary:result];
            }else{
                pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:data];
            }
        }
        [self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
    }];
}

-(UMSocialPlatformType) getPlatform:(NSNumber *)platform{
    if([platform isEqualToNumber:[[NSNumber alloc] initWithInt:WEIXIN]]) {
        return UMSocialPlatformType_WechatSession;
    } else if ([platform isEqualToNumber:[[NSNumber alloc] initWithInt:QQ]]) {
        return UMSocialPlatformType_QQ;
    } else if ([platform isEqualToNumber:[[NSNumber alloc] initWithInt:WEIBO]]) {
        return UMSocialPlatformType_Sina;
    } else {
        return UMSocialPlatformType_UnKnown;
    }
}
@end
